package history

/**
 * @Description: 英语流利说一面
 * @Keyword: 统计配对加和等于目标值的数量
 * @Author: kami
 * @Date: 2022/6/8 16:01
 **/
func count(a []int, sum int) int {
	var cnt int
	var left = 0
	var right = len(a) - 1
	for left < right {
		var tmpSum = a[left] + a[right]
		switch {
		case tmpSum > sum:
			right--
		case tmpSum < sum:
			left++
		case tmpSum == sum:
			var tmpLeft = left + 1
			var leftValue = a[left]
			var leftOffSet = 1
			for tmpLeft < right {
				if a[tmpLeft] == leftValue {
					leftOffSet++
					tmpLeft++
					continue
				}
				break
			}

			var tmpRight = right - 1
			var rightValue = a[right]
			var rightOffSet = 1
			for left < tmpRight {
				if a[tmpRight] == rightValue {
					rightOffSet++
					tmpRight--
					continue
				}
				break
			}
			cnt += (leftOffSet * rightOffSet)

			left = tmpLeft
			right = tmpRight
		}
	}
	return cnt
}
